<?php
namespace app\admin\controller;

use app\admin\traits\Curd;
use app\common\constants\AdminConstant;
use app\common\controller\AddonBase;
use think\facade\Request;
use app\common\controller\AdminController;
use think\facade\Event;

class Addon extends AddonBase
{
    use Curd;
    protected $thisControllerJsPath;

    public function __construct()
    {
        $app = app();
        parent::__construct(app());
        $this->request = $app->request;
        $controller = Request::controller();
        $this->thisControllerJsPath = request()->domain() . '/'.env('mango.addons_static').'/addons/'.$this->name.'/js/'.$controller.'.js';
        $this->thisControllerJs = file_exists('../addons/'.$this->name.'/js/'.$controller.'.js');
        $this->handle();
        $this->view->assign(['version' => time()]);
        $this->adminController = new AdminController($app);
        $a = '\\addons\\'.$this->name.'\\model\\'.$controller;
        if(class_exists($a)){
            $this->model = new $a();
        }
        // 监听检查插件事件
        Event::trigger('CheckAddons', $this->name);
    }

    public function handle()
    {
        list($thisModule, $thisController, $thisAction) = [app('http')->getName(), Request::controller(), Request::action()];
        list($thisControllerArr, $jsPath)               = [explode('.', $thisController), null];
        foreach ($thisControllerArr as $vo) {
            empty($jsPath) ? $jsPath = parse_name($vo) : $jsPath .= '/' . parse_name($vo);
        }
        $autoloadJs = $this->thisControllerJs ? true : false;
        $thisControllerJsPath = $this->thisControllerJsPath;
        $adminModuleName      = config('app.admin_alias_name');
        $isSuperAdmin         = session('admin.id') == AdminConstant::SUPER_ADMIN_ID ? true : false;
        $data                 = [
            'adminModuleName'      => $adminModuleName,
            'thisController'       => parse_name($thisController),
            'thisAction'           => $thisAction,
            'thisRequest'          => parse_name("{$thisModule}/{$thisController}/{$thisAction}"),
            'thisControllerJsPath' => "{$thisControllerJsPath}",
            'autoloadJs'           => $autoloadJs,
            'isSuperAdmin'         => $isSuperAdmin,
            'version'              => env('app_debug') ? time() : ConfigService::getVersion(),
        ];
        // dump($data);exit;
        $this->view->assign($data);
    }







}
